import uuid
import psycopg2
# load the psycopg extras module
#import psycopg2.extras

#reference https://wiki.postgresql.org/wiki/Psycopg2_Tutorial

########################################################################################################################

class PostgresDb:

    def __init__(self, connection_string = None):
        self.connection_string = connection_string
        pass

    def get(self, sql, data = None):
        conn = psycopg2.connect(self.connection_string)
        cur = conn.cursor()
        cur.execute(sql, data)
        rows = cur.fetchall()
        conn.close()
        return rows

    def run(self, sql, data):
        conn = psycopg2.connect(self.connection_string)
        cur = conn.cursor()
        cur.execute(sql, data)
        conn.commit()
        conn.close()
        return cur.rowcount

    def test(self):
        return "PostgresDb works"


########################################################################################################################

etracks = PostgresDb("dbname='etracks' user='zhixian.ong' host='localhost' password='p@ssw0rd1'")

if __name__ == '__main__':
    try:
        conn = psycopg2.connect("dbname='etracks' user='zhixian.ong' host='localhost' password='p@ssw0rd1'")

        cur = conn.cursor()

        cur.execute("""SELECT * FROM student;""")

        rows = cur.fetchall()

        print "\nShow me the databases:\n"
        print len(rows)
        #print len(rows[0])

        #conn.set_isolation_level(0) # to drop database


    except:
        print "I am unable to connect to the database"
    pass
